﻿// ===============================================================================
// Project Name        :    Gfw.BLL.Basic.Facilities
// Project Description :    
// ===============================================================================
// Class Name          :    ToECurrency
// Namespace:          :    Gfw.BLL.Basic.Facilities
// Class Version       :    v1.0.0.0
// Class Description   :    
// CLR Version:        :    4.0.30319.17626
//Author               :    lif
// Machine             :    ADICO-PC
// Create Time         :    2012/7/8 10:37:46
// Update Time         :    2012/7/8 10:37:46
// ===============================================================================
// Copyright © lif 2012 . All rights reserved.
// ===============================================================================

using System;
using Gfw.Infrastructure.Account;
using Gfw.Infrastructure.ExceptionImpl;
using Gfw.Models.Account;
using Gfw.Repositories.Account;
using Gfw.Repository;

namespace Gfw.Infrastructure.Facilities
{
    /// <summary>
    /// 现金币转换到电子币功能事务
    /// </summary>
    public class ToECurrencyFacility:  IFacility
    {
        private string _userId;
        private decimal _count;
        private ActUserAccountinformation _userAccount;

        public ToECurrencyFacility(string userId,decimal count)
        {
            if (count <= 0)
                throw new GfwVerifyException("转账的参数不正确！");
            _count = count;
            _userId = userId;
        }
        public ToECurrencyFacility(ActUserAccountinformation userAccount, decimal count)
        {
           
            if (count <= 0)
                throw new GfwVerifyException("转账的参数不正确！");
            _count = count;
            _userAccount = userAccount;
           
        }


        public void Execute(Repository.UnitOfWork unitOfWork = null)
        {
            UnitOfWork work = unitOfWork ?? new UnitOfWork();
            ActUserAccountinformationRepository accountRep = null;
            if (_userAccount == null)
            {
                 accountRep = new ActUserAccountinformationRepository();
                 _userAccount = accountRep.Get(_userId, work);
            }

        
            if (_userAccount.Cash < _count)
                throw new GfwVerifyException("余额不足!");

            _userAccount.Cash -= _count;
            _userAccount.ElectronicMoney += _count;

            DateTime tradeDate = DateTime.Now;


            ActUserBargain4cash cachHistory = new ActUserBargain4cash()
                                                  {
                                                      Id = Guid.NewGuid().ToString(),
                                                      Outlay = _count,
                                                      Income = 0,
                                                      Balance = _userAccount.Cash,
                                                      
                                                      TradeDate = tradeDate,
                                                      TradeAbstract = "X币转D币",
                                                      TradeType = (int) UserTradeTypeEnum.TransferOut,
                                                      Userid = _userAccount.Userid,
                                                      AwardSerialNo = ""
                                                  };
            ActUserBargain4emoney emoneyHistory = new ActUserBargain4emoney()
                                                      {
                                                          Id = Guid.NewGuid().ToString(),
                                                          AwardSerialNo = "",
                                                          Balance = _userAccount.ElectronicMoney,
                                                          Income = _count,
                                                          Outlay = 0,
                                                          Taxation = 0,
                                                          TaxationSerialNo = "",
                                                          TradeAbstract = "X币转D币",
                                                          TradeDate = tradeDate,
                                                          TradeType = (int) UserTradeTypeEnum.TransferIn,
                                                          Userid = _userAccount.Userid

                                                      };
            ActUserBargain4cashRepository cashRep = new ActUserBargain4cashRepository();
            ActUserBargain4emoneyRepository emnoeyRep = new ActUserBargain4emoneyRepository();
         
            cashRep.Save(cachHistory, work);
            emnoeyRep.Save(emoneyHistory, work);
             //作为单事务，需要提交更改
            if (unitOfWork == null)
            {
                accountRep.Update(_userAccount, work);
                work.Commit();
            }

        }
    }
}
